<!--~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  ~ Copyright (c) 2017-2018 Contributors to the Eclipse Foundation
  ~
  ~ See the NOTICE file(s) distributed with this work for additional
  ~ information regarding copyright ownership.
  ~
  ~ This program and the accompanying materials are made available under the
  ~ terms of the Eclipse Public License 2.0 which is available at
  ~ http://www.eclipse.org/legal/epl-2.0, or the Apache Software License 2.0
  ~ which is available at https://www.apache.org/licenses/LICENSE-2.0.
  ~
  ~ SPDX-License-Identifier: EPL-2.0 OR Apache-2.0
  ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~-->

<div class="right">
    <a href="{{srcPath}}">
        <button class="btn btn-primary" name="zip">ZIP</button>
    </a>
</div>
<div *ngIf="!loading">
    <div>
        <winery-uploader (onSuccess)="loadFiles()" [uploadUrl]="uploadUrl" [allowMultipleFiles]="true"
                         [isEditable]="sharedData?.currentVersion?.editable">
        </winery-uploader>
    </div>
    <br>
    <div id="fileGridHeader">
        <h5 id="gridHeaderText">Already included Files:</h5>
        <button id="gridHeaderNewBtn" class="btn btn-sm btn-toolbar"
                [disabled]="!sharedData?.currentVersion?.editable"
                (click)="onCreateNewFile()">
            Add New
        </button>
        <button class="btn btn-sm btn-toolbar"
                [disabled]="selectedFile == null"
                (click)="undoFileChanges()">
            Reload from Server
        </button>
        <button class="btn btn-sm btn-danger" [disabled]="selectedFile == null" (click)="removeElementModal.show();">
            Delete
        </button>
        <button class="btn btn-sm btn-toolbar" [disabled]="selectedFile == null" (click)="openRenameDialog()">
            Rename
        </button>
        <span></span>
        <button *ngIf="enableCopyToFiles" id="gridHeaderPostToFiles" class="btn btn-sm btn-toolbar"
                (click)="copyAllSrc()">
            Copy all to Files
        </button>
        <button class="btn btn-sm btn-primary"
                [disabled]="!sharedData?.currentVersion?.editable || selectedFile == null"
                (click)="saveEditorContent()">
            Save
        </button>
    </div>
    <div id="fileGridBody">
        <winery-loader *ngIf="loadingFileContent"></winery-loader>
        <div>
            <div id="leftEditContent">
                <div *ngFor="let path of paths" class="fileList">
                    <div *ngIf="path != ''" (click)="selectPath(path)"
                         [ngClass]="{selected: path === selectedPath, fileListFolder: true}">
                        <span></span>
                        <span>{{path}}</span>
                    </div>
                    <div class="fileListSubFiles" *ngIf="selectedPath == path || path == ''"
                         [ngClass]="{indented: path != ''}">
                        <div *ngFor="let file of fileMap.get(path)"
                             [ngClass]="{selected: file.name === selectedFile?.name, fileListFile: true}"
                             (click)="editFile(file)">
                            <span class="preview"><img src="{{file.thumbnailUrl}}"></span>
                            <span class="name">{{file.name}}</span>
                            <span class="size">{{file.size / 1024 | number: '1.2-2'}} KB</span>
                        </div>
                    </div>
                </div>
            </div>
        </div>
        <div id="rightEditHeader">
            <span *ngIf="fileContent != null && (fileContent !== editor?.getData())">*</span>
            {{selectedFile?.name}}
        </div>
        <div id="rightEditContent">
            <winery-editor #artifactsEditor></winery-editor>
        </div>
    </div>
</div>


<winery-modal bsModal #removeElementModal="bs-modal" [modalRef]="removeElementModal">
    <winery-modal-header [title]="'Delete File'"></winery-modal-header>
    <winery-modal-body>
        <p>
            Are you sure you want to remove <span style="font-weight:bold;">{{ selectedFile?.name }}</span>?
        </p>
    </winery-modal-body>
    <winery-modal-footer [closeButtonLabel]="'Cancel'" [okButtonLabel]="'Delete'"
                         [okButtonClass]="'btn-danger'"
                         (onOk)="onRemoveElement()">
    </winery-modal-footer>
</winery-modal>

<winery-modal bsModal #saveCurrentFileModal="bs-modal" [modalRef]="saveCurrentFileModal">
    <winery-modal-header [title]="'Save Changes'"></winery-modal-header>
    <winery-modal-body>
        <p>
            There are unsaved changes to the currently opened File,
            do you want to save them?
        </p>
    </winery-modal-body>
    <winery-modal-footer [closeButtonLabel]="'Cancel'" [okButtonLabel]="'Save'"
                         (onOk)="saveEditorContent()">
        <button class="btn btn-default" name="undoChanges" (click)="undoFileChanges()">Revert Changes</button>
    </winery-modal-footer>
</winery-modal>

<winery-modal bsModal #createNewFileModal="bs-modal" [modalRef]="createNewFileModel">
    <winery-modal-header [title]="'Create New File'"></winery-modal-header>
    <winery-modal-body>
        <form #addNewArtifactFileForm="ngForm" id="createFile">
            <div class="form-group">
                <label class="control-label" for="fileName">Name</label>
                <input #fileNameInput="ngModel"
                       id="fileName"
                       class="form-control"
                       name="fileName"
                       required
                       [(ngModel)]="newFileName"
                       [wineryDuplicateValidator]="validatorObject"
                />
                <div *ngIf="fileNameInput.errors && (fileNameInput.dirty || fileNameInput.touched)"
                     class="alert alert-danger">
                    <div [hidden]="!fileNameInput.errors.wineryDuplicateValidator">
                        No duplicates allowed!
                    </div>
                    <div [hidden]="!fileNameInput.errors.required">
                        Name is required!
                    </div>
                </div>
                <br>
                <label class="control-label" for="filePath">Path</label>
                <input #filePathInput="ngModel"
                       id="filePath"
                       class="form-control"
                       name="filePath"
                       [(ngModel)]="newFileDir"
                       [wineryDuplicateValidator]="pathValidatorObject"
                />
                <div *ngIf="filePathInput.errors && (filePathInput.dirty || filePathInput.touched)"
                     class="alert alert-danger">
                    <div [hidden]="addNewArtifactFileForm?.form.valid">
                        The path must either be empty or of the form <b>path\to</b>
                    </div>
                </div>
            </div>
        </form>
    </winery-modal-body>
    <winery-modal-footer [closeButtonLabel]="'Cancel'" [okButtonLabel]="'Create'"
                         (onOk)="createNewFile()"
                         [disableOkButton]="!(addNewArtifactFileForm?.form.valid && newFileName?.length > 0)">
    </winery-modal-footer>
</winery-modal>

<winery-modal bsModal #renameFileModal="bs-modal" [modalRef]="renameFileModal">
    <winery-modal-header [title]="'Rename File'"></winery-modal-header>
    <winery-modal-body>
        <form #addImplForm="ngForm" id="renameFile">
            <div class="form-group">
                <label class="control-label" for="name">Name</label>
                <input #localNameInput="ngModel"
                       id="name"
                       class="form-control"
                       name="name"
                       required
                       [(ngModel)]="renameFileName"
                       [wineryDuplicateValidator]="validatorObject"
                />
                <div *ngIf="localNameInput.errors && (localNameInput.dirty || localNameInput.touched)"
                     class="alert alert-danger">
                    <div [hidden]="!localNameInput.errors.wineryDuplicateValidator">
                        No duplicates allowed!
                    </div>
                    <div [hidden]="!localNameInput.errors.required">
                        Name is required!
                    </div>
                </div>
            </div>
        </form>
    </winery-modal-body>
    <winery-modal-footer [closeButtonLabel]="'Cancel'" [okButtonLabel]="'Rename'"
                         (onOk)="renameSelection()">
    </winery-modal-footer>
</winery-modal>
